package com.dongao.activity.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import com.dongao.model.Mp3Info;
import com.dongao.utils.FileUtil;
import com.dongao.utils.HttpDownloadUtil;

public class DownloadService extends Service {
	


	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("服务启动");
		Mp3Info mp3Info=(Mp3Info) intent.getSerializableExtra("mp3Info");
		System.out.println("1");
		Thread thread=new Thread(new DownloadThread(mp3Info));
		System.out.println("2");
		thread.start();
		System.out.println("3");
		return super.onStartCommand(intent, flags, startId);
	}
	
	//下载操作在HttpDownloadUtil种已实现
	//但是为了显示下载进度所以在这从写了
	class DownloadThread implements Runnable {
		private Mp3Info mp3Info = null;
		public DownloadThread(Mp3Info mp3Info) {
			this.mp3Info = mp3Info;
		}
		
		@Override
		public void run() {
			String urlMp3 = "http://videodl.dongao.com/2013jxjy/ydb/wsj/xjkzl/wk/13cedu_wsjxjkzl_001_1_yd/upload/media/wsj_jxjy_xjkzl_01_580x435_180kbps.m3u8";
			String fileName = "东奥.m3u8";
			String path = "/dongao/video";
			Integer result = null;
			if (FileUtil.isFileExist(fileName, path)) {
				result = 1;
			} else {
				InputStream inputStream = null;
				File file = null;
				OutputStream output = null;
				try {
					inputStream = HttpDownloadUtil.getInputStreamFromUrl(urlMp3);
					
					FileUtil.createSDDir(path);
					file = FileUtil.createFileInSDCard(fileName, path);
					output = new FileOutputStream(file);
					byte buffer[] = new byte[4 * 1024];
					int temp;
					while((temp = inputStream.read(buffer)) != -1) {
						output.write(buffer,0,temp);
					}
					output.flush();
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{
					if(output!=null) {
						try {
							output.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
				if(file == null) {
					result = -1;
				} else {
					result = 0;
				}
			}
		}
	}

	/*class DownloadThread implements Runnable {
		private Mp3Info mp3Info = null;
		public DownloadThread(Mp3Info mp3Info) {
			this.mp3Info = mp3Info;
		}
		
		@Override
		public void run() {
//			String urlMp3 = getText(resId);
			String urlMp3 = "http://120.193.11.26/rdown/hot_s/2008-05/28/13841/2008052800901.mp3?pvt=2";
			System.out.println("开始下载");
			int result = HttpDownloadUtil.downloadFile(urlMp3, "/Sounds/Digital", "放心不下3.mp3");
			System.out.println(result);
			String resultMessage = null;
			switch(result) {
			case 1:
				resultMessage = "文件下载成功";
				break;
			case 0:
				resultMessage = "文件已存在";
				break;
			case -1:
				resultMessage = "文件下载失败";
				break;
			default:
				break;
			}
			
//			Toast.makeText(getApplicationContext(), resultMessage, Toast.LENGTH_SHORT).show();  
		}
	}*/
}
